<?php
namespace app\cms\controller;
use framework\ext\Check;
/* *
 * 内容管理
 * */
class IndexController extends \app\base\controller\AdminController{

    //文章列表
    public function index(){
        $act = form('get.act');
        if ($act == "alldel") {//批量删除
            $all_id = form('post.fid',array('all'));
            if (is_array($all_id)) {
                $counts = count($all_id['fid']);
                $i = 0;
                foreach ($all_id as $value) {//开始删除            
                    $result = model('index')->_delete(intval($value));
                    if($result)$i++;
                }
                $this->pageMsg('共选择 '.$counts.' 条内容，成功删除 '.$i.' 请返回',1,url('index/index'));
            }else{
                $this->pageMsg('没有选择要删除的内容');
            }
        }elseif($act == 'del'){
            $id = form('get.id',0,intval);
            $result = model('index')->_delete($id); 
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败');
            }
        }else{
            $this->cid = form('get.id',0,intval);
            $page = form('get.page',0,intval);
            $this->path = model('channel')->path($this->cid,'index/index');
            $this->list = model('index')->pager($page,20)->_select($this->cid,$this->website_id);
            $this->pager = $this->getPage(model('index')->pager,array(id=>$this->cid));
            $this->display();
        }
    }

    //添加内容
    public function add(){
        $this->cid = form('get.id',0,intval);
        $root = model('channel')->_find(array('id'=>$this->cid),'id,pid,rootid,mid');
        if(!$root){
            $this->pageMsg('栏目不存在!请返回重试。');
        }else{
            if($root['pid'] == 0){
                $this->rootid = $root['id'];
            }else{
                $this->rootid = $root['rootid'];
            }
            $this->mid = $root['mid'];
            $this->editor = $_SESSION[$this->appID . '_userInfo']['username'];
            $this->path = model('channel')->path($this->cid,'index/index'); //当前路径
            $this->module = model('field')->fromInput($this->mid);
            $this->display(); 
        }
    }

    //编辑内容
    public function edit(){
        $this->fid = form('get.id',0,intval);
        $this->info = model('index')->_edit($this->fid);
        if (!$this->info) {
            $this->pageMsg('未找到编辑的资源');
        }
        $this->path = model('channel')->path($this->info['channel_id'],'index/index');
        $root =  model('channel')->_find(array('id'=>$this->info['channel_id']),'rootid,mid,id');
        if($root['rootid'] == 0){
            $this->rootid = $root['id'];
        }else{
            $this->rootid = $root['rootid'];
        }
        $this->mid = $root['mid'];
        $this->module =  model('field')->fromInput($this->mid,TRUE,$this->info); 
        $this->display();
    }

    //保存内容
    public function save(){
        if($this->isPost()){
            $fid = form('post.fid',0,intval);
            $cid = form('post.channel_id',0,intval); 
            $mid = form('post.mid',0,intval);
            $data = form(post,array('fid','mid','special'));
            $data['uptime'] = time();
            $data['special'] =$_POST['special'];
            $rel = model('index')->dataSave($mid,$data,$fid);
            $this->redirect(url('index/index',array('cid'=>$cid)));
        }
    }

    //状态更改
    public function state(){
        $uid = form('get.uid',0,intval);
        $rel = model('index')->_find(array('id' => $uid),'status');
        $data['status'] = $rel['status']?0:1;
        $rel = model('index')->_update($data,array('id'=>$uid));
        if($rel){
            echo 'ok';
        }
    }
    
    //###########################################
    //扩展列表内容
    public function exts(){
        $act = $_GET['act'];
        if($act == 'del'){
            $cid = form('get.cid',0,intval);
            $fid = form('get.fid',0,intval);
            $result = model('index')->extsdelete($cid,$fid); 
            if($result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg('删除失败');
            }
        }else{
            $id = form('get.id',0,intval);
            $this->cid = form('get.cid',0,intval);
            if(!$id || !$this->cid){
                $this->pageMsg('参数出错,请返回访问');
            }
            $page = form('get.page',0,intval);
            $this->info = model('index')->_find(array('id' =>$id),'title,id');
            $this->path = model('channel')->path($this->cid,'index/index');
            $this->list = model('index')->pager($page,20)->exts_select($this->cid,$id);
            $this->pager = $this->getPage(model('index')->pager,array(cid=>$this->cid,id=>$id));
            $this->display();
        }
    }

    //添加和编辑扩展
    public function extsedit(){
        $this->cid = form('get.cid',0,intval);
        $this->id = form('get.id',0,intval);
        $this->fid = form('get.fid',0,intval);
        $this->path = model('channel')->path($this->cid,'index/index');
        $this->info = model('index')->_find(array('id' =>$this->id));
        $this->editor = $_SESSION[$this->appID . '_userInfo']['username'];
        $channel =  model('channel')->_find(array('id'=>$this->cid),'plus_mid,id');
        if($this->fid){
            $this->rel = model('index')->extsedit($channel['plus_mid'],$this->fid);
            if($this->rel){
                $this->module = model('field')->fromInput($channel['plus_mid'],TRUE,$this->rel); 
            }  
        }else{
            $this->module = model('field')->fromInput($channel['plus_mid']); 
        }
        $this->display();
    }

    //保存
    public function extssave(){
        $channel_id = form('post.channel_id',0,intval);
        $content_id = form('post.content_id',0,intval);
        $fid = form('post.fid',0,intval);
        $data = form('post',array('fid'));
        $rel = model('index')->extssave($data,$channel_id,$content_id,$fid);
        if($rel){
            $this->redirect(url('index/exts',array(cid=>$channel_id,id=>$content_id)));
        }else{
            $this->pageMsg('添加失败！返回重试');
        }
    }
}